<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<script th:inline="javascript">
	var userField = '[(${userField})]';

	if (userField != '') {
		function initAtUserFieldChange[(${field})]() {
			var oldValue_[(${userField})] = ""; // 一个不存在的值
			var sint = setInterval(function(){
				if (o("[(${userField})]")) {
					if (oldValue_[(${userField})] != o("[(${userField})]").value) {
						oldValue_[(${userField})] = o("[(${userField})]").value;
						onUserChange_[(${field})]();
					}
				} else {
					window.clearInterval(sint);
				}
			},500);
		}
		initAtUserFieldChange[(${field})]();

		function onUserChange_[(${field})]() {
			var ajaxData = {
				formCode: "[(${formCode})]",
				userName: $("[name='[(${userField})]']").val(),
				field: "[(${field})]",
				roleCodes: '[(${roles})]',
			}

			$.ajax({
				type: "post",
				url: "../../flow/macro/getRoleUserRelatedToUser",
				contentType:"application/x-www-form-urlencoded; charset=iso8859-1",
				data: ajaxData,
				dataType: "json",
				beforeSend: function(XMLHttpRequest){
				},
				success: function(res, status){
					console.log('getRoleUserRelatedToUser', res);
					if (res.code === 200) {
						if (res.data.res === 0) {
							$(o("[(${field})]")).val(res.data.userName);
						}
					}
				},
				complete: function(XMLHttpRequest, status){
				},
				error: function(XMLHttpRequest, textStatus){
					// 请求出错处理
					alert(XMLHttpRequest.responseText);
				}
			});
		}
	}
</script>
</body>
</html>